// 引入redux相关依赖
import { createStore } from 'redux'

// 初始化数据
const initState = {
  name: 'Tom',
  age: 18
}

// 创建action，是obj类型的数据
// let changeAge = { type: 'changeAge', payload: 20 };
// let changeName = { type: 'changeName', payload: 'Jack' };

// 创建reducer,是一个纯函数，用来修改state
const reducer = (state = initState, action) => {
  switch (action.type) {
    // 根据action的type判断，修改对应的数据
    case 'changeAge':
      // 返回新的数据state
      return {
        ...state,
        age: action.payload
      }
    case 'changeName':
      // 返回新的数据state
      return {
        ...state,
        name: action.name
      }

    default:
      return state
  }
}

// 创建store
const store = createStore(reducer)

export default store